

var crypto = require('../utils/crypto');
var express = require('express');
var db = require('../utils/db');
var http = require('../utils/http');

var app = express();

var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
  extended: true
}));
app.set('jwtTokenSecret', 'gy_liliang_waibao');

var hallAddr = '';

function send(res, ret) {
  var str = JSON.stringify(ret);
  res.send(str);
}

var config = null;
// 创建 application/x-www-form-urlencoded 编码解析

exports.start = function (cfg) {
  config = cfg;
  hallAddr = config.SHOP_IP + ':' + config.CLEINT_PORT;
  app.listen(config.CLEINT_PORT);
  console.log('account server is listening on ' + config.CLEINT_PORT);
};

// 设置跨域访问
app.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'X-Requested-With');
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
  res.header('X-Powered-By', ' 3.2.1');
  res.header('Content-Type', 'application/json;charset=utf-8');
  next();
});

/**获取所有商品 */
app.post('/shop_getAllShop', function (req, res, next) {
  db.get_all_shop(function (ret) {
    if (ret !== null) {
      console.log('获取所有商品成功');
      send(res, { shopData: ret });
    } else {
      send(res, { shopData: null });
      console.log('获取所有商品失败');
    }
  });
});

/**获取玩家背包 */
app.post('/shop_GetUserShop', function (req, res, next) {
  db.get_user_shop(req.body.accountId, function (ret) {
    if (ret !== null) {
      console.log('获取玩家背包成功');
      send(res, { shopData: ret });
    } else {
      console.log('获取玩家背包无商品');
      send(res, { shopData: null });
    }
  });
});

/**玩家购买商品 */
app.post('/shop_addUserShop', function (req, res, next) {
  var accountId = req.body.accountId;
  var goodsId = req.body.shopId;

  var userMOney;
  db.get_user_Money(accountId, function (ret) {
    if (ret !== null) {
      userMOney = ret[0].userMoney;
      var goodsMoney;
      db.get_Goods_Money(goodsId, function (ret) {
        if (ret !== null) {
          goodsMoney = ret[0].shopMoney;
          if (userMOney >= goodsMoney) {
            var userMon = userMOney - goodsMoney;
            console.log("玩家金币 " + userMOney + " 商品价格 " + goodsMoney);
            db.update_user_Money(userMon, accountId, function (ret) {
              db.add_user_shop(accountId, goodsId, function (ret) {
                console.log('购买商品成功  商品Id' + goodsId);
                send(res, { isBuy: true, goodsId: goodsId });
              });
            });
          }
          else {
            send(res, { isBuy: false });
            return false;
          }
        }
      });
    }
  });
});

/**更新玩家装扮 */
app.post('/shop_updataAdressUp', function (req, res, next) {
  var accountId = req.body.accountId;
  var decorateId = req.body.decorateId;
  var trailingId = req.body.trailingId;
  db.update_user_dressup(accountId, decorateId, trailingId, function (ret) {
    if (ret == null) {
      console.log('更新装扮成功');
      send(res, { isUpdata: true });
    } else {
      console.log('更新装扮失败');
      send(res, { isUpdata: false });
    }
  });
});

/**用于商店获取玩家装扮 */
app.post('/shop_getUserAdressUp', function (req, res, next) {
  var accountId = req.body.accountId;

  db.check_user_exist(accountId, function (ret) {
    if (ret == null) {
      console.log('无玩家  新建了一个玩家');
      db.add_user_dressUp(accountId, function (ret) {
        if (ret == null) {
          console.log('创建成功.');
          db.get_user_dressUp(accountId, function (ret) {
            if (ret !== null) {
              console.log('获取玩家装扮成功');
              var data = ret[0];
              var sign = crypto.md5(data.AccountId + data.DecorateId + data.TrailingId + data.userMoney + config.ROOM_PRI_KEY);
              ret[0].sign = sign;
              send(res, { dressData: ret });
            } else {
              console.log('获取玩家装扮失败');
              send(res, { dressData: null });
            }
          });
        }
      });
    }
    else {
      db.get_user_dressUp(accountId, function (ret) {
        if (ret !== null) {
          console.log('获取玩家装扮成功');
          var data = ret[0];
          var sign = crypto.md5(data.AccountId + data.DecorateId + data.TrailingId + data.userMoney + config.ROOM_PRI_KEY);
          ret[0].sign = sign;
          send(res, { dressData: ret });
        } else {
          console.log('获取玩家装扮失败');
          send(res, { dressData: null });
        }
      });
    }
  });
});

app.post('/user_shareSuccess', function (req, res, next) {
  var accountId = req.body.accountId;
  var userData;
  db.get_user_dressUp(accountId, function (ret) {
    if (ret !== null) {
      userData = ret[0];
      var numberOfShare = userData.userNumberOfShare + 1;
      db.update_user_shareSuccess(numberOfShare, accountId, function (ret) {
        if (ret == null) {
          if (numberOfShare == 6) {
            db.add_user_shop(accountId, 1004, function (ret) {
            });
            return;
          }
          if (numberOfShare == 4) {
            db.add_user_shop(accountId, 1003, function (ret) {
            });
            return;
          }
          if (numberOfShare == 2) {
            db.add_user_shop(accountId, 1002, function (ret) {
            });
          }

        }
      });
    } else {
      console.log('获取玩家数据失败');
      send(res, { dressData: null });
    }
  });

});

